<?php

require_once 'classes/LightPressPlugin.php';

class SearchIndex extends LightPressPlugin {

    var $constructor_args = array(
        'posts_per_page'=>'number of entries to show');
    var $default_context = 160; // LP_CONTEXT_SEARCH | LP_CONTEXT_TAG;
    var $description = 'Modify the layout of search results';
    

    var $active = true;
    var $hooks = array('get_posts', 'parse_post');
    
    var $posts_per_page = 30;

    function SearchIndex(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run) {
            
            $frontend->options['posts_per_page'] = $this->posts_per_page;
            
            // build the feed URL
            $context_url = $frontend->context['url'];
            if ($frontend->raw_urls)
                $feed_url = str_replace('index.php', 'feed.php', $context_url);
            else
                $feed_url = $context_url . '/rss2.xml';
            
            $frontend->tpl->setVar('search_feed_url', $feed_url);

        }
    }

    function hide() {
        return;
    }
    
    function run($hook, &$post) {
        if (!$this->active)
            return;
        $this->_frontend->tpl->setVar('search_keywords_encoded', urlencode($this->_frontend->context['filter']));
        $kw =& $this->_frontend->search_keywords;
        if ($hook == 'get_posts')
            return;
        $content = '';
        foreach (explode(' ', strip_tags($post['post_content'])) as $word) {
            if (strlen("$content $word") > 204)
                break;
            $content .= " $word";
        }
        if (is_array($kw)) {
            foreach ($kw as $k)
                $content = preg_replace("/($k)/i", '<span class="searchkeyword">\1</span>', $content);
        }
        $post['post_content'] = $content;
    }

}

?>
